package com.xishu.service.print.kitchen;

import com.xishu.config.Config;
import com.xishu.entity.order.Order;
import com.xishu.util.Tools;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class InitCmd implements Command, Tools {
    private Config config = Config.getInstance();
    private static Logger logger = LoggerFactory.getLogger(InitCmd.class);
    private List<Long> orderItemIdList;
    private Order orderInDb;

    public InitCmd(Order orderInDb, List<Long> orderItemIdList) {
        this.orderInDb = orderInDb;
        this.orderItemIdList = orderItemIdList;
    }

    @Override
    public boolean execute(Context context) throws Exception {
        if (!config.canPrint()) {
            logger.info("can not print");
            return true;
        }

        if (isEmpty(orderItemIdList)) {
            return true;
        }

        if (orderInDb == null) {
            logger.info("order in db is null");
            return true;
        }

        if (isEmpty(orderItemIdList)) {
            logger.info("orderItemIdList is empty");
            return true;
        }

        logger.info("will print shop {} order {}, orderItemIdList {}", orderInDb.getShopId(), orderInDb.getId(), StringUtils.join(orderItemIdList, ","));

        return false;
    }
}
